package com.youtellv1.mapper;

import com.youtellv1.entity.ChildPay;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ChildPayMapper extends BaseMapper<ChildPay>{
    @Select("select * from tb_childpay order by cpid limit #{offset}, #{limit}")
    List<ChildPay> findAllChildPay(@Param("offset") Integer offset, @Param("limit") Integer limit);

    @Select("select * from tb_childpay where cid = #{cid} order by cpid limit #{offset}, #{limit}")
    List<ChildPay> findChildPayByCidLimit(@Param("cid") Integer cid,
                                          @Param("offset") Integer offset,
                                          @Param("limit") Integer limit);

    // 查询当前页数据
    @Select({
            "<script>",
            "SELECT * FROM tb_childpay WHERE cid = #{cid}",
            "<if test='startDate != null and endDate != null'>",
            "AND cpstart &gt;= #{startDate} AND cpstart &lt;= #{endDate}",
            "</if>",
            "ORDER BY cpstart DESC",
            "LIMIT #{offset}, #{size}",
            "</script>"
    })
    List<ChildPay> findPage(@Param("cid") Integer cid,
                            @Param("startDate") String startDate,
                            @Param("endDate") String endDate,
                            @Param("offset") int offset,
                            @Param("size") int size);

    // 查询总记录数
    @Select({
            "<script>",
            "SELECT COUNT(*) FROM tb_childpay WHERE cid = #{cid}",
            "<if test='startDate != null and endDate != null'>",
            "AND cpstart &gt;= #{startDate} AND cpstart &lt;= #{endDate}",
            "</if>",
            "</script>"
    })
    int count(@Param("cid") Integer cid,
              @Param("startDate") String startDate,
              @Param("endDate") String endDate);


    @Select("select count(*) from tb_childpay where cid = #{cid}")
    Integer countByCid(@Param("cid") Integer cid);


    @Select("select count(*) from tb_childpay")
    Integer childPayCount();

    @Select("select * from tb_childpay where cpid = #{cpid}")
    ChildPay findById(@Param("cpid") Integer cpid);

    @Select("select * from tb_childpay where cpid = #{cpid}")
    ChildPay findChildPayById(ChildPay childPay);

    @Insert("insert into tb_childpay(cpstart, cpend, cpway, cmoney, cid) values(#{cpstart}, #{cpend}, #{cpway}, #{cmoney}, #{cid})")
    void save(ChildPay childPay);

    @Update("<script>" +
            "update tb_childpay " +
            "<set>" +
            "    <if test='cpstart != null'>cpstart = #{cpstart},</if>" +
            "    <if test='cpend != null'>cpend = #{cpend},</if>" +
            "    <if test='cpway != null'>cpway = #{cpway},</if>" +
            "    <if test='cmoney != null'>cmoney = #{cmoney},</if>" +
            "    <if test='cid != null'>cid = #{cid},</if>" +
            "</set>" +
            "where cpid = #{cpid}" +
            "</script>")
    void update(ChildPay childPay);

    @Delete("delete from tb_childpay where cpid = #{cpid}")
    void delete(ChildPay childPay);

    @Select("select count(distinct cid) as pay_person_count from tb_childpay")
    Integer countChild();

    @Select("select sum(cmoney) as total_payment from tb_childpay")
    Float countChildPay();
}
